package com.bestv.xml.parser;

import com.bestv.database.vo.IncrSeries;
import com.bestv.utils.common.Constants;

import java.io.File;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by Henry on 14-7-9.
 */
public class SeriesParser extends Parser {
    public SeriesParser() {
        xpathExpression = "//Objects/Object[@ElementType='Series']";
    }

    public IncrSeries parse(File file) {
        Map<String, String> map = toMap(file);
        if (map == null) return null;

        IncrSeries series = new IncrSeries();
        series.setMediaCode(map.get("Code"));
        series.setName(map.get("Name"));
        series.setType(map.get("Type"));
        series.setGenre(map.get("Genre"));
        series.setTags(map.get("Tags"));
        series.setKeywords(map.get("Keywords"));
        series.setActors(map.get("ActorDisplay"));
        series.setDirector(map.get("WriterDisplay"));

        String sourceType = map.get("SourceType");
        if (sourceType != null) {
            series.setSourceType(Short.valueOf(sourceType));
        } else {
            series.setSourceType(null);
        }

        series.setRegion(map.get("OriginalCountry"));
        series.setLanguage(map.get("Language"));
        series.setVolumnCount(Integer.valueOf(map.get("VolumnCount")));
        series.setReleaseYear(map.get("ReleaseYear"));

        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            Date addedTime = df.parse(map.get("UpdateTime"));
            series.setAddedTime(new Timestamp(addedTime.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (Constants.XML_ACTION_DELETE.equals(map.get("Action"))) {
            series.setStatus(Constants.SERIES_STATUS_OFFLINE);
        } else {
            series.setStatus(Constants.SERIES_STATUS_NORMAL);
        }

        series.setPrice(Double.valueOf(map.get("Price")));
        return series;
    }
}
